package core.form.action;

import java.io.IOException;

import javax.inject.Inject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ActionSupport;

import core.base.action.BasePageRequest;
import core.form.model.Form;
import core.form.service.FormService;

@Controller
@Scope("prototype")
public class FormAction extends ActionSupport {

	private Page pager;
	

	private Form content;

	private String modelId;

	@Inject
	private FormService formService;

	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -4699430773947179617L;

	public Form getContent() {
		return content;
	}

	public void setContent(Form content) {
		this.content = content;
	}

	public String create() {
		return SUCCESS;
	}

	public String edit() {
		try {
			setContent(formService.findForm(modelId));
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return INPUT;
		}

		return SUCCESS;
	}

	public String save() {
		try {
			formService.saveOrUpdateForm(content);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return INPUT;
		}

		return SUCCESS;
	}

	public String list() {
		try {
			BasePageRequest pageRequest = (BasePageRequest) BasePageRequest.bindRequest(ServletActionContext.getRequest());
			Page pager = formService.listForms(pageRequest);
			try {
				setPager(pager);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return INPUT;
		}

		return SUCCESS;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}
}
